#ifndef _XSEL_CLIPBOARD
#define _XSEL_CLIPBOARD

#ifndef WIN32

#ifndef _CLIPBOARD
#include "clipboard.hpp"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>

#include <X11/Xmu/Atoms.h>
#include <X11/Xmu/StdSel.h>

#include <X11/Xaw/Command.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xfuncs.h>

#ifdef XKB
#include <X11/extensions/XKBbells.h>
#endif

class xsel_clipboard: public clipboard
{
public:

	inline xsel_clipboard() {};
	xsel_clipboard(itoken* _token);
xsel_clipboard(itoken *_itoken, std::vector<unsigned short> _idx);


	int write_buffer();	
	int clear_buffer();

private:

	int read_buffer(); // to print text out of clipboard buffer
	int run();
	int init();

	static int read_stdin(char**);
	static int write_stdout(char* _str, int len);
	static int read_fd(int fd, char** result);
	static int write_fd(int fd, char* string, int len);
	static void quit(Widget w, XtPointer closure = NULL, XtPointer callData = NULL);
	
	static Boolean convert_selection(Widget w, Atom *selection, Atom *target, Atom *type, XtPointer *value, unsigned long *length, int *format);

	static void store_buffer(Widget w, XtPointer client_data, Atom *selection, Atom *type, 
		XtPointer value, unsigned long *length, int *format);

	static void lose_selection(Widget w, Atom *selection);
	static void get_selection(Widget w, XtPointer closure= NULL, XtPointer callData =NULL);

	static void get_buffer(Widget w, XtPointer closure, XtPointer callData =NULL);

	/*
	int parse_args(int argc, char *argv[]); 	// unused
	int init(int argc, char *argv[]);		// unused
	void syntax(char *call);  			// unused
	*/

	static bool XtSetAppExitFlag;
	static void XtAppMainLoop(const XtAppContext& appContext);
	
	static const int ALLOCCHUNK = 1024;
	static XrmOptionDescRec* optionDesc;
#define Offset(field) XtOffsetOf(OptionsRec, field)
	static Atom selection;
	static char* tmpbuffer; 		
	static int tmpbuflen; 			
	static int selection_current;
	static int do_paste;
	static int do_copy;
	static int do_merge;
	static String selection_type; // "PRIMARY";

};


#endif

#endif
